//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.member;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberTransferDao;
import com.aidong.model.wills.MemberCard;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.MemberTransfer;
import com.aidong.model.wills.PayList;
import com.aidong.web.controller.member.PayController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"memberTransfer"})
public class MemberTransferController extends PayController {
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    MemberTransferDao memberTransferDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;

    public MemberTransferController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("member/transfer/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.contractCode", contractCode);
        cd.eq("c.memberCode", memberCode);
        if(!this.chkNull(dateFrom)) {
            cd.ge("c.saleDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("c.saleDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.memberTransferDao.queryPageList(pg, cd);
        ModelAndView mv = new ModelAndView("member/transfer/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("member/transfer/add");
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("gender", this.getTypeList("gender"));
        mv.addObject("idType", this.getTypeList("idType"));
        mv.addObject("_picPath", this.getSetting("pic.path"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("contract") MemberTransfer transfer, @ModelAttribute("member") MemberInfo member, @RequestParam("payls") String payls) {
        if(this.chkNull(transfer.getContractCode())) {
            return this.getResult(false, "请输入合同号");
        } else if(this.chkNull(transfer.getNewMemberType())) {
            return this.getResult(false, "请选择转入会员类型");
        } else if(this.chkNull(transfer.getNewCardCode())) {
            return this.getResult(false, "请输入会员卡号");
        } else {
            MemberContract oldContract = this.memberContractDao.getByContractCode(transfer.getContractCode());
            if(oldContract == null) {
                return this.getResult(false, "合同号错误");
            } else {
                Date beginTime = oldContract.getBeginTime();
                Date endTime = oldContract.getEndTime();
                if(beginTime != null && endTime != null && oldContract.getStatus().intValue() == 1 && oldContract.getEffectiveDate() == null && !"8".equals(oldContract.getSaleType())) {
                    if(oldContract.getEndTime().getTime() < System.currentTimeMillis()) {
                        return this.getResult(false, "合同已过有效期,不能转让");
                    } else {
                        double leftAmount;
                        if(oldContract.getTotalCount().intValue() > 0) {
                            leftAmount = (double)Math.round((double)oldContract.getRestCount().intValue() * oldContract.getFactPrice().doubleValue() / (double)oldContract.getTotalCount().intValue());
                        } else if(beginTime.getTime() > System.currentTimeMillis()) {
                            leftAmount = oldContract.getFactPrice().doubleValue();
                        } else {
                            leftAmount = (double)Math.round((double)(endTime.getTime() - System.currentTimeMillis()) * oldContract.getFactPrice().doubleValue() / (double)(endTime.getTime() - beginTime.getTime()));
                        }

                        if(leftAmount > oldContract.getFactPrice().doubleValue()) {
                            leftAmount = oldContract.getFactPrice().doubleValue();
                        }

                        if(leftAmount <= 0.0D) {
                            leftAmount = 0.0D;
                        }

                        MemberInfo payList;
                        if("2".equals(transfer.getNewMemberType())) {
                            if(this.chkNull(transfer.getNewMemberCode())) {
                                return this.getResult(false, "请输入转入会员编号");
                            }

                            if(oldContract.getMemberCode().equalsIgnoreCase(transfer.getNewMemberCode())) {
                                return this.getResult(false, "转入会员不能和原会员相同");
                            }

                            payList = this.memberInfoDao.getByMemberCode(transfer.getNewMemberCode());
                            if(payList == null) {
                                return this.getResult(false, "转入会员错误");
                            }

                            payList.setMemberPhoto(member.getMemberPhoto());
                            payList.setIdCardPhoto(member.getIdCardPhoto());
                            payList.setMobile(member.getMobile());
                            member = payList;
                        } else {
                            if(this.chkNull(member.getChName())) {
                                return this.getResult(false, "请输入会员姓名");
                            }

                            if(this.chkNull(member.getIdType())) {
                                return this.getResult(false, "请输入证件类型");
                            }

                            if(this.chkNull(member.getIdNumber())) {
                                return this.getResult(false, "请输入证件号");
                            }

                            if(this.chkNull(member.getMobile())) {
                                return this.getResult(false, "请输入手机号");
                            }

                            payList = this.memberInfoDao.getByIdNumber(member.getIdType(), member.getIdNumber());
                            if(payList != null) {
                                if(oldContract.getMemberCode().equalsIgnoreCase(payList.getMemberCode())) {
                                    return this.getResult(false, "转入会员不能和原会员相同");
                                }

                                payList.setMemberPhoto(member.getMemberPhoto());
                                payList.setIdCardPhoto(member.getIdCardPhoto());
                                payList.setMobile(member.getMobile());
                                member = payList;
                            } else {
                                member.setMemberCode((String)null);
                                member.setCardCode((String)null);
                                member.setMemberSource("01");
                                member.setShopCode(this.getLoginShopCode());
                                member.setStatus(Integer.valueOf(1));
                                member.setCreateUser(this.getLoginUserCode());
                                member.setCreateTime(new Date());
                                member.setUpdateTime(new Date());
                            }
                        }

                        if(!this.chkNullZ(transfer.getEarnestId())) {
                            MemberEarnest payList1 = (MemberEarnest)this.memberEarnestDao.getById(transfer.getEarnestId());
                            if(payList1 == null || payList1.getStatus().intValue() != 0 || !payList1.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                                return this.getResult(false, "定金不可用");
                            }

                            transfer.setEarnestAmount(payList1.getEarnestAmount());
                        } else {
                            transfer.setEarnestId((Integer)null);
                            transfer.setEarnestAmount(Double.valueOf(0.0D));
                        }

                        transfer.setSaleDate(new Date());
                        transfer.setShopCode(this.getLoginShopCode());
                        transfer.setContractCode(oldContract.getContractCode());
                        transfer.setMemberCode(oldContract.getMemberCode());
                        transfer.setCardCode(oldContract.getCardCode());
                        transfer.setNewMemberCode(member.getMemberCode());
                        if(this.chkNull(member.getCardCode()) || !member.getCardCode().equalsIgnoreCase(transfer.getNewCardCode())) {
                            MemberCard payList2 = this.memberCardDao.getById(transfer.getNewCardCode());
                            if(payList2 == null || !payList2.getShopCode().equalsIgnoreCase(this.getLoginShopCode()) || payList2.getStatus().intValue() != 1) {
                                return this.getResult(false, "会员卡号错误");
                            }

                            transfer.setNewCardCode(payList2.getCardCode());
                        }

                        transfer.setAddFee(oldContract.getChangeFee());
                        transfer.setPayAmount(Double.valueOf(transfer.getAddFee().doubleValue() - transfer.getEarnestAmount().doubleValue()));
                        if(transfer.getPayAmount().doubleValue() < 0.0D) {
                            transfer.setPayAmount(Double.valueOf(0.0D));
                        }

                        transfer.setStatus(Integer.valueOf(1));
                        transfer.setSignStatus(Integer.valueOf(this.chkNullZ(transfer.getSignStatus())?2:0));
                        transfer.setPrintLabel(Integer.valueOf(0));
                        transfer.setCreateUser(this.getLoginUserCode());
                        transfer.setCreateTime(new Date());
                        transfer.setUpdateTime(new Date());
                        ArrayList payList3 = new ArrayList();
                        StringBuilder payMsg = new StringBuilder();
                        PayList pay = this.getPayList(payls, payList3, "10", payMsg);
                        if(pay == null) {
                            return this.getResult(false, payMsg.toString());
                        } else if(!this.equals(pay.getAmount().doubleValue(), transfer.getPayAmount().doubleValue())) {
                            return this.getResult(false, "付款金额不一致");
                        } else {
                            transfer.setSalesCode(pay.getSalesCode());
                            transfer.setSalesCode2(pay.getSalesCode2());
                            new SimpleDateFormat("yyyy-MM-dd");
                            Date newEndTime = oldContract.getNewEndTime();
                            if(beginTime.getTime() < System.currentTimeMillis()) {
                                beginTime = this.getTrimDate(new Date());
                            }

                            MemberContract contract;
                            if(!this.chkNull(member.getMemberCode())) {
                                contract = this.memberContractDao.getByMemberCode(member.getMemberCode());
                                if(contract != null && contract.getEndTime().getTime() > System.currentTimeMillis()) {
                                    Date dt2 = new Date(contract.getEndTime().getTime() + 86400000L);
                                    int dd = (int)((dt2.getTime() - beginTime.getTime()) / 1000L / 60L / 60L / 24L);
                                    beginTime = dt2;
                                    Calendar cal = Calendar.getInstance();
                                    cal.setTime(endTime);
                                    cal.add(5, dd);
                                    endTime = cal.getTime();
                                    if(newEndTime != null) {
                                        cal.setTime(newEndTime);
                                        cal.add(5, dd);
                                        newEndTime = cal.getTime();
                                    }
                                }
                            }

                            contract = new MemberContract();
                            contract.setShopCode(this.getLoginShopCode());
                            contract.setMemberCode(member.getMemberCode());
                            contract.setCardCode(member.getCardCode());
                            contract.setSaleType("4");
                            contract.setPromotionId((Integer)null);
                            contract.setEarnestId((Integer)null);
                            contract.setEarnestAmount(Double.valueOf(0.0D));
                            contract.setSaleDate(new Date());
                            contract.setBeginTime(beginTime);
                            contract.setEndTime(endTime);
                            contract.setNewEndTime(newEndTime);
                            contract.setTotalCount(oldContract.getRestCount());
                            contract.setRestCount(oldContract.getRestCount());
                            contract.setCardType(oldContract.getCardType());
                            contract.setManyShop(oldContract.getManyShop());
                            contract.setValids(oldContract.getValids());
                            contract.setValidUnit(oldContract.getValidUnit());
                            contract.setAddValids(oldContract.getAddValids());
                            contract.setAddValidUnit(oldContract.getAddValidUnit());
                            contract.setAddFee(oldContract.getAddFee());
                            contract.setFixedFee(oldContract.getFixedFee());
                            contract.setStopFee(oldContract.getStopFee());
                            contract.setChangeFee(oldContract.getChangeFee());
                            contract.setMendFee(oldContract.getMendFee());
                            contract.setChgShopFee(oldContract.getChgShopFee());
                            contract.setContDiscount(oldContract.getContDiscount());
                            contract.setCardPrice(Double.valueOf(leftAmount));
                            contract.setTotalPrice(Double.valueOf(leftAmount));
                            contract.setFactPrice(Double.valueOf(leftAmount));
                            contract.setPayAmount(Double.valueOf(0.0D));
                            contract.setInAmount(Double.valueOf(leftAmount));
                            contract.setStatus(Integer.valueOf(1));
                            contract.setOldContractCode(transfer.getContractCode());
                            contract.setCommissionDate(new Date());
                            contract.setReceivePaper(Integer.valueOf(0));
                            contract.setBarcodeTasteContract(Integer.valueOf(0));
                            contract.setSignStatus(Integer.valueOf(2));
                            contract.setPrintLabel(Integer.valueOf(0));
                            contract.setSalesCode(oldContract.getSalesCode());
                            contract.setSalesCode2(oldContract.getSalesCode2());
                            contract.setCreateUser(this.getLoginUserCode());
                            contract.setCreateTime(new Date());
                            contract.setUpdateTime(new Date());
                            contract.setReviewStatus(Integer.valueOf(0));

                            try {
                                this.memberTransferDao.addTransfer(transfer, payList3, contract, member);
                                this.smsListService.addSmsList("17", transfer.getSaleId().intValue());
                                this.smsListService.addSmsList("18", contract.getSaleId().intValue());
                                this.addUserLog("会员转让", "合同号:" + contract.getContractCode() + ",会员:" + contract.getMemberCode());
                            } catch (Exception var19) {
                                this.log.error("add transfer", var19);
                                return this.getResult(false, "保存发生错误");
                            }

                            return this.getSuccess(String.valueOf(transfer.getSaleId()), "保存完成");
                        }
                    }
                } else {
                    return this.getResult(false, "无效合同");
                }
            }
        }
    }
}
